<template>
    <div class="login-container">
        <div class="login-box">
        <div class="logo">
            <img src="../assets/logo.png" alt="">
        </div>
            <el-form label-width="40px" class="login-form" :model='loginForm' :rules="rules" ref="loginFormRef">
                <el-form-item label="" prop="username">
                    <el-input prefix-icon="iconfont icon-yonghuming" v-model='loginForm.username'></el-input>
                </el-form-item>
                <el-form-item label="" prop="password">
                    <el-input prefix-icon="iconfont icon-mima" v-model='loginForm.password' type="password"></el-input>
                </el-form-item>
                <el-row class="btns">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="info" @click="resetLoginForm">重置</el-button>
                </el-row>
            </el-form>
  </div>
    </div>
</template>

<script>
export default {
    data(){
        return{
            loginForm:{
                username:'admin',
                password:'123456',
            },
            rules:{
                username:[
                    {
                        required:true,
                        message:'请输入用户名',
                        trigger:'blur'
                    },{
                        max:10,
                        min:2,
                        message:'用户名长度在2-10之间',
                        trigger:'blur'
                    }
                ],
                password:[
                    {
                        required:true,
                        message:'请输入密码',
                        trigger:'blur'
                    }
                ]
            },
        }
    },
    methods:{
        //重置表单数据
        resetLoginForm() {
            this.$refs.loginFormRef.resetFields();
        },
        //二次验证
        login(){
            this.$refs.loginFormRef.validate(async valid=>{
                if (valid) {
                 var {data:response}=await this.$http.post('login',this.loginForm);
                 console.log(response);
                 if (response.meta.status!='200') {
                     this.$message({
                         type:'error',
                         message:response.meta.msg,
                         center:true,
                         offset: 30
                     })
                 }else{
                     this.$message({
                         type:'success',
                         message:response.meta.msg,
                         center:true,
                         offset: 30
                     })
                     window.sessionStorage.setItem('token',response.data.token);
                     this.$router.push('/home')
                 }
                }
            })
        },

    }
}
</script>

<style lang='less' scoped>
.loginFont{
    font-size: 12px
}
.login-container{
    background-color: #2b4b6b;
    height: 100%;
}
.login-box{
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    width: 450px;
    height: 300px;
    background: #fff;
    border-radius: 3px;
}
.logo{
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    width: 100px;
    height: 100px;
    border-radius: 50%;
    overflow: hidden;
    padding: 20px;
    background-color: #fff;
    box-shadow: 0 0 10px #ddd;
    img{
        width: 100%;
        height: 100%;
    }
}
.login-form{
    position: absolute;
    width: 100%;
    bottom:10px;
    padding: 0 10px;
    box-sizing: border-box;
}
.btns{
    display: flex;
    justify-content: flex-end;
}
</style>
